package com.test.controller.admin;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.test.controller.Admininfo.ResData;
import com.test.entity.HrInfo;
import com.test.service.HrInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 人力资源(HrInfo)表控制层
 *
 * @author makejava
 * @since 2022-11-12 11:18:51
 */
@Controller
public class AdminHrInfoController {

    @Autowired
    private HrInfoService hrInfoService;

    //    显示页面
    @GetMapping("admin/hr_info")
    public String getHrInfo() {
        return "admin/hr_info/list";
    }

    //    显示页面中的HR信息
    @GetMapping("admin/hr_info/queryList")
    @ResponseBody
    public String getQueryList(Integer page, HrInfo hrInfo) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hrInfoService.queryByPage(hrInfo, PageRequest.of(page - 1, 10)));
    }

    //    新增
    @GetMapping("admin/hr_info/add")
    public String getAdd() {
        return "admin/hr_info/add_page";
    }

    //    提交新增
    @PostMapping("admin/hr_info/add_submit")
    @ResponseBody
    public ResData postAddSubmit(HrInfo hrInfo) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        hrInfo.setCreateTime(simpleDateFormat.format(new Date()));
        ResData resData = new ResData();
        HrInfo insert = hrInfoService.insert(hrInfo);
        if (insert.getId() != null || !insert.getId().equals("")) {
            resData.setCode(200);
            resData.setMsg("添加成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("添加失败");
        return resData;
    }

    //    修改
    @GetMapping("admin/hr_info/update")
    public ModelAndView getUpdate(Integer id) {
        ModelAndView modelAndView = new ModelAndView("admin/hr_info/update_page");
        HrInfo hrInfo = hrInfoService.queryById(id);
        modelAndView.addObject("data", hrInfo);
        return modelAndView;
    }

    //    提交修改
    @PostMapping("admin/hr_info/update_submit")
    @ResponseBody
    public ResData postUpdateSubmit(HrInfo hrInfo) {
        ResData resData = new ResData();
        Integer update = hrInfoService.update(hrInfo);
        if (update > 0) {
            resData.setCode(200);
            resData.setMsg("修改成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("修改失败");
        return resData;
    }

    //    删除
    @GetMapping("admin/hr_info/del")
    @ResponseBody
    public ResData getDelete(Integer id) {
        ResData resData = new ResData();
        boolean b = hrInfoService.deleteById(id);
        if (b) {
            resData.setCode(1);
            resData.setMsg("删除成功");
            return resData;
        }
        resData.setCode(0);
        resData.setMsg("删除失败");
        return resData;
    }

}

